package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.slice.slice.NotificationAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.event.notification.NotificationHelper;
import ohos.event.notification.NotificationRequest;
import ohos.event.notification.NotificationSlot;
import ohos.rpc.RemoteException;

import static ohos.event.notification.NotificationHelper.cancelNotification;

public class NotificationAbility extends Ability implements Component.ClickedListener {

    private Button sendTextBtn;
    private Button sendPicBtn;
    private Button sendConversationBtn;
    private Button cancelBtn;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(NotificationAbilitySlice.class.getName());
        setUIContent(ResourceTable.Layout_ability_notifiaction);

        sendTextBtn = (Button) findComponentById(ResourceTable.Id_sengTextMsg);
        sendTextBtn.setClickedListener(this);

//        sendPicBtn = (Button) findComponentById(ResourceTable.Id_sengPicMsg);
//        sendPicBtn.setClickedListener(this);
//
//        sendConversationBtn = (Button) findComponentById(ResourceTable.Id_sengConversationMsg);
//        sendConversationBtn.setClickedListener(this);

        cancelBtn = (Button) findComponentById(ResourceTable.Id_cancelMsg);
        cancelBtn.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_sengTextMsg:
                initNotification();
                break;
//            case ResourceTable.Id_sengPicMsg:
//                sendPicMsg();
//                break;
//            case ResourceTable.Id_sengConversationMsg:
//                sendConversationMsg();
//                break;
            case ResourceTable.Id_cancelMsg:
                cancelNotification();
                break;
        }
    }

    private void cancelNotification() {
        int notificationId = 1001;
        try {
            NotificationHelper.cancelNotification(notificationId);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

//    private void sendConversationMsg() {
//        // Implementation here
//    }
//
//    private void sendPicMsg() {
//        // Implementation here
//    }

    private void initNotification() {

        NotificationSlot slot = new NotificationSlot("slot_001", "my_slot", NotificationSlot.LEVEL_DEFAULT);
        slot.setDescription("发布通知");
        slot.setEnableVibration(true);
        slot.setEnableLight(true);
        slot.setLedLightColor(Color.RED.getValue());
        try {
            NotificationHelper.addNotificationSlot(slot);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }

        int notificationId = 1001;
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(slot.getId());

        String title = "登录成功";
        String text = "欢迎您登录“有点is”";
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(title);
        content.setText(text);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }
}
